use std::{collections::HashMap, vec};

use bevy::prelude::*;
use bincode::{Decode, Encode};

///作物天数
#[derive(Resource, Deref, DerefMut, Encode, Decode, PartialEq, Debug, Clone)]
pub struct PlantingDay {
    #[deref]
    pub day: usize,
    pub time: usize,
}

impl Default for PlantingDay {
    fn default() -> Self {
        PlantingDay { day: 1, time: 420 }
    }
}

impl PlantingDay {
    pub fn new() -> Self {
        PlantingDay { day: 1, time: 0 }
    }
    ///天数
    pub fn next_day(&mut self) {
        self.day += 1;
        self.time = 0;
    }

    pub fn get_time_str(&self) -> String {
        let mut str = String::new();
        let mut hour = self.time / 60;
        let mut minute = self.time % 60;
        if hour > 24 {
            hour = 24;
            minute = 0;
        }
        str.push_str(format!("{:0>2}:{:0>2}", hour, minute).as_str());
        str
    }
}

#[derive(Clone, Deref, DerefMut)]
pub struct FieldData {
    #[deref]
    pub field_entity: Entity,
    pub water_entity: Option<Entity>,
    pub crop_entity: Option<Entity>,
}

impl FieldData {
    pub fn new(field_entity: Entity) -> Self {
        FieldData {
            field_entity,
            water_entity: None,
            crop_entity: None,
        }
    }
    ///获取作物
    pub fn get_crop(&self) -> Option<Entity> {
        self.crop_entity
    }

    ///设置作物
    pub fn set_crop(&mut self, entity: Entity) {
        self.crop_entity = Some(entity);
    }

    ///删除作物
    pub fn del_crop(&mut self) {
        self.crop_entity = None;
    }

    ///获取水
    pub fn get_water(&self) -> Option<Entity> {
        self.water_entity
    }

    ///设置水
    pub fn set_water(&mut self, entity: Entity) {
        self.water_entity = Some(entity);
    }

    ///是否浇水
    pub fn is_water(&self) -> bool {
        self.water_entity.is_some()
    }

    ///干涸
    pub fn dry(&mut self) {
        self.water_entity = None;
    }
}

///块地图
#[derive(Resource, Deref, DerefMut)]
pub struct BlockMap {
    #[deref]
    pub field_map: HashMap<IVec2, FieldData>,
    pub furniture_map: HashMap<IVec2, Entity>,
}

//作物方法
impl BlockMap {
    pub fn new() -> Self {
        BlockMap {
            field_map: HashMap::new(),
            furniture_map: HashMap::new(),
        }
    }

    ///设置地块
    pub fn set_field(&mut self, x: i32, y: i32, entity: Entity) {
        if self.field_map.contains_key(&IVec2::new(x, y)) {
            println!("尝试设置一个已经存在的地块pos:x:{}y:{}", x, y);
            return;
        }
        self.field_map
            .insert(IVec2::new(x, y), FieldData::new(entity));
    }

    ///获取地块
    pub fn get_field(&mut self, x: i32, y: i32) -> Option<&mut FieldData> {
        self.field_map.get_mut(&IVec2::new(x, y))
    }

    pub fn get_all_field(&mut self) -> &mut HashMap<IVec2, FieldData> {
        &mut self.field_map
    }
}

//家具方法
impl BlockMap {
    ///设置家具
    pub fn set_furniture(&mut self, x: i32, y: i32, entity: Entity) {
        if self.furniture_map.contains_key(&IVec2::new(x, y)) {
            println!("尝试设置一个已经存在的家具pos:x:{}y:{}", x, y);
            return;
        }
        self.furniture_map.insert(IVec2::new(x, y), entity);
    }

    ///获取家具
    pub fn get_furniture(&mut self, x: i32, y: i32) -> Option<&Entity> {
        self.furniture_map.get(&IVec2::new(x, y))
    }
}

///背包
#[derive(Resource)]
pub struct MasterBackpack {
    pub entitys: Vec<Option<Entity>>,
    pub max_count: usize,
    pub show_count: usize,
    pub select_index: usize,
    pub offset: usize,
}

impl MasterBackpack {
    pub fn new() -> Self {
        MasterBackpack {
            entitys: vec![None; 30],
            max_count: 30,
            select_index: 0,
            show_count: 9,
            offset: 0,
        }
    }

    pub fn select_up(&mut self) {
        if self.select_index > 0 {
            self.select_index -= 1;
        } else {
            if self.offset > 0 {
                self.offset -= 1;
            }
        }
    }

    pub fn select_down(&mut self) {
        if self.select_index < self.show_count - 1 {
            self.select_index += 1;
        } else {
            if self.offset + self.show_count - 1 < self.max_count - 1 {
                self.offset += 1;
            } else {
                self.select_index = 0;
                self.offset = 0;
            }
        }
    }
}

///快捷栏
#[derive(Resource)]
pub struct FastList {
    pub entitys: Vec<Option<Entity>>,
    pub max_count: usize,
    pub select_index: usize,
}

impl FastList {
    pub fn new() -> Self {
        let fast_list = FastList {
            entitys: vec![None; 9],
            max_count: 9,
            select_index: 0,
        };
        fast_list
    }

    pub fn select_up(&mut self) {
        if self.select_index > 0 {
            self.select_index -= 1;
        } else {
            self.select_index = self.max_count;
        }
    }

    pub fn select_down(&mut self) {
        if self.select_index < self.max_count - 1 {
            self.select_index += 1;
        } else {
            self.select_index = 0;
        }
    }
}

